package com.hui.li.util;

import com.hui.li.entity.User;
import org.apache.commons.collections4.Put;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @projectName: Spring-RestTemplate
 * @package: com.hui.li.util
 * @className: RestTemplateTest
 * @author: Hui
 * @date: 2021/12/22 21:02
 */
public class RestTemplateTest {
    public static void main(String[] args) {
    }

    private static void allMethodRequest() {
        //发送get请求(get,post默认发送的是json数据)
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        // 需求:请求的时候使用请求头     put和delete需要返回值
        String path = "http://localhost:8080/hui02/{0}";
        //创建请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        //加入token
        httpHeaders.add("token","3jkl3jmh3h3ij3ijh3iho3.5ji5jhojn5klhoi55hi5o5hk5.7jio7o7ih7jh7uh7l7hi");
        //把请求头封装成HttpEntity类型
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        //HttpMethod.DELETE根据实际情况改变（HttpMethod.GET）......
        ResponseEntity<String> exchange = restTemplate.exchange(path, HttpMethod.DELETE, httpEntity, String.class, "1,2,3");
        System.out.println(exchange);
    }

    private static void deleteForData() {
        //发送get请求(get,post默认发送的是json数据)
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        //远程接口的路径
        String path = "http://localhost:8080/hui02/{0}";
        restTemplate.delete(path,"1,2,3,4,5,6");
    }

    private static void putForData() {
        //发送get请求(get,post默认发送的是json数据)
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        //远程接口的路径
        String path = "http://localhost:8080/hui1";
        Put<String,Object> map = new HashedMap<>();
        map.put("name","小橙子");
        map.put("age",19);
        restTemplate.put(path,map);
    }

    private static void postForData() {
        //发送get请求(get,post默认发送的是json数据)
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        //远程接口的路径
        String path = "http://localhost:8080/hui";
        User user = new User();
        user.setName("橙子");
        user.setAge(18);
        //执行post请求获取响应体信息
        String s = restTemplate.postForObject(path, user, String.class);
        System.out.println(s);
        //执行post请求获取所有响应信息
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(path, user, String.class);
        System.out.println(stringResponseEntity);
    }

    private static void getForObjectRestful() {
        //发送get请求(get,post默认发送的是json数据)
        //get请求 restful风格
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        //远程接口路径
        String phone = "18236865165";
        //远程接口的路径
        String path = "http://localhost:8080/hui/{0}";
        //最后一个参数是可变参数，可放多个
        String forObject = restTemplate.getForObject(path, String.class, phone);
        System.out.println(forObject);
    }

    private static void getForEntity() {
        //发送get请求(get,post默认发送的是json数据)
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        //远程接口路径
        String url = "http://api.tianapi.com/qingshi/index?key=ea49a0a1765263099647ad5f542e24eb";
        //获取的全部内容
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        //获取响应头的信息
        System.out.println("响应头="+forEntity.getHeaders());
        //响应体的信息
        System.out.println("响应体的信息="+forEntity.getBody());
        //响应行的信息
        System.out.println("响应行的信息="+forEntity.getStatusCode());
    }

    private static void getForObject() {
        //发送get请求(get,post默认发送的是json数据)
        //创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        //远程接口路径
        String url = "http://api.tianapi.com/qingshi/index?key=ea49a0a1765263099647ad5f542e24eb";
        //获取的响应体的内容
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println(forObject);
    }
}
